package com.py.credit.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.FastEntrance;
import com.py.credit.entity.SysUser;
import com.py.credit.service.FastEntranceService;
import com.py.credit.service.RemoteBaseService;
import com.py.framework.core.entity.MenuEntity;
import com.py.framework.core.helper.HttpServletHelper;

/**
 * 快捷入口controller
 * 
 * @author likai
 *
 */
@RestController
@RequestMapping(value = "/fastEntrance")
public class FastEntranceController {
	@Autowired
	private FastEntranceService fastEntranceService;

	@Autowired
	private RemoteBaseService remoteBaseService;

	/**
	 * 添加字典
	 * 
	 * @param resourceId
	 * @return
	 * @throws ParameterException
	 */
	@RequestMapping(value = "/add.json")
	public RestServiceResult<String> add(HttpServletRequest request,
			@RequestParam(defaultValue = "", required = true, value = "menuList") String menuList) {
		RestServiceResult<String> result = new RestServiceResult<String>();
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		long userId = 0;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo && userInfo.getId() > 0) {

				userId = userInfo.getId();

			} else {

				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("添加失败,未获取到当前用户信息！");
				ExceptionLogger.error("FastEntranceController#add", "未获取到当前用户信息！");
				return result;

			}

		} else {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("添加失败,未获取到当前用户信息！");
			ExceptionLogger.error("FastEntranceController#add", "未获取到当前用户信息！");
			return result;

		}

		JSONArray menuArray = JSON.parseArray(menuList);
		fastEntranceService.removeFastEntrances(userId);

		for (int i = 0; i < menuArray.size(); i++) {
			JSONObject menu = menuArray.getJSONObject(i);

			fastEntranceService.add(Integer.parseInt(menu.getString("id")), menu.getString("subSysId"), userId,
					menu.getString("image"), i, menu.getString("name"), menu.getString("sysTitle"),
					menu.getString("url"));

		}

		return result;

	}

	/**
	 * 获取用户所有快捷入口
	 * 
	 * @return
	 */
	@RequestMapping(value = "/list.json")
	public RestServiceResult<List<FastEntrance>> getFastEntranceListInfoByCode(HttpServletRequest request) {

		RestServiceResult<List<FastEntrance>> result = new RestServiceResult<List<FastEntrance>>();
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			long userId;

			SysUser userInfo = user.getData();

			if (null != userInfo && (userId = userInfo.getId()) > 0) {

				List<FastEntrance> list = fastEntranceService.getList(userId);

				result.setData(list);

				return result;

			} else {

				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("添加失败,未获取到当前用户信息！");
				ExceptionLogger.error("FastEntranceController#getFastEntranceListInfoByCode", "未获取到当前用户信息！");
				return result;

			}

		} else {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("添加失败,未获取到当前用户信息！");
			ExceptionLogger.error("FastEntranceController#getFastEntranceListInfoByCode", "未获取到当前用户信息！");
			return result;

		}

	}

	/**
	 * 当前用户资源菜单列表
	 * 
	 * @param name
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/resourceMenuList.json")
	public RestServiceResult<List<Map<String, Object>>> getList(
			@RequestParam(value = "name", defaultValue = "", required = true) String name, HttpServletRequest request) {
		List<Map<String, Object>> res = new ArrayList<Map<String, Object>>();
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		RestServiceResult<List<MenuEntity>> currentResourceMenus = remoteBaseService
				.getCurrentResourceMenus(HttpServletHelper.getUserToken(request));
		List<MenuEntity> resources = new ArrayList<MenuEntity>();
		if (RestServiceResult.CODE_SUCCESS == currentResourceMenus.getCode()
				&& currentResourceMenus.getData() != null) {
			resources = currentResourceMenus.getData();
		}

		for (MenuEntity resource : resources) {

			Map<String, Object> resourceMenu = new HashMap<String, Object>();
			resourceMenu.put("id", resource.getId());
			resourceMenu.put("subSysId", resource.getParentId());
			resourceMenu.put("name", resource.getMenuName());
			resourceMenu.put("sysTitle", resource.getParentMenuName());
			resourceMenu.put("url", resource.getRouteAddr());

			res.add(resourceMenu);

		}

		result.setData(res);

		return result;

	}

}
